#include <iostream>
#include "TcpClient.h"
using namespace std;

TcpClient::TcpClient(const std::string& serv_addr, uint32_t port)
{
	if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
        perror("Socket creation failed");
    }

    m_sockaddr_in_.sin_family = AF_INET;
    m_sockaddr_in_.sin_port = htons(port);

    // 将IP地址从字符串转换为网络字节序
    if(inet_pton(AF_INET, serv_addr.c_str(), &m_sockaddr_in_.sin_addr)<=0) {
        perror("Invalid address/Address not supported");
    }

	    // 连接到服务器
    if (connect(sock, (struct sockaddr *)&m_sockaddr_in_, sizeof(m_sockaddr_in_)) < 0) {
        perror("Connection Failed");
    }
}

int TcpClient::net_read(char* buffer, int size)
{
	read(sock, buffer, size);
}

int TcpClient::net_write(char* buffer,int length)
{
	// 发送消息到服务器
    send(sock, buffer, length, 0 );
}

TcpClient::~TcpClient()
{
}